//
//  MusicPlayerViewController.m
//  哎游
//
//  Created by 康守帅 on 15-1-8.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MusicPlayerViewController.h"
#import "Singleton.h"
#import "MusicModel.h"
#import "UIImageView+WebCache.h"
#import "MusicDataBase.h"
#import "LoginViewCnntroller.h"

@interface MusicPlayerViewController ()
{
    double time;
}

@end

@implementation MusicPlayerViewController

- (void)dealloc
{
    [_mid release];
    [_musicArray release];
    [_name release];
    [_circleBtn release];
    [_aboveBtn release];
    [_playBtn release];
    [_nextBtn release];
    [_progressSlider release];
    [_controlView release];
    [_maskView release];
    [_currentTimeLabel release];
    [_totalTimeLabel release];
    [_imageUrl release];
    [_playModeView release];
    [_backGroundImageView release];
    [_tempId release];
    [_progressUpdateTimer release];
    [_playModeLabel release];
    [_playModeTimer release];
    [_enjoyBtn release];
    [_music release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.musicArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.isClickPlayMode = NO;
    self.isClickEnjoy = NO;
    self.playMode = isOrder;
    [self createView];
    [self createPlayerView];
    [self startMonitor];
    [self createPlayModeView];
    [self swipe];
    [self createEnjoyButton];
    [self judgeMusicExist];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self selectMusicIndex];
    [self judgeMusicExist];
    
    [_backGroundImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    if (self.sameMusic == NO) {
        Singleton *share = [Singleton shareInstance];
        share.musicPlayerViewController.progressSlider.value = 0;
        share.musicPlayerViewController.currentTimeLabel.text = nil;
        share.musicPlayerViewController.totalTimeLabel.text = nil;
        share.musicPlayerViewController.name.text = nil;
        [self playMusic];
    }
}

- (void)selectMusicIndex
{
    Singleton *share = [Singleton shareInstance];
    for (int i = 0; i < [_musicArray count]; i++) {
        
        MusicModel *model = [_musicArray objectAtIndex:i];
        if ([share.musicId isEqualToString:[model.trackId stringValue]]) {
            _index = i;
            _music = model;
            break;
        }
    }
}

#pragma mark -
#pragma mark 视图创建区
- (void)createView
{
    UIButton *down = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, 20, 60, 20)];
    [down setImage:[UIImage imageNamed:@"xiala2.png"] forState:UIControlStateNormal];
    
    [down addTarget:self action:@selector(downAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:down];
    [down release];
    
    self.backGroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 120 - 120)];
    
    [self.view addSubview:_backGroundImageView];
    [_backGroundImageView release];
    
    self.name = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 80, self.view.frame.size.height - 120 - 35, 160, 20)];
    _name.textColor = [UIColor whiteColor];
    _name.font = [UIFont systemFontOfSize:15];
    _name.backgroundColor = [UIColor clearColor];
    _name.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_name];
    [_name release];
}

// 创建音乐控制界面视图
- (void)createPlayerView
{
    self.controlView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 100)];
    _controlView.backgroundColor = [UIColor blackColor];
    self.maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    _maskView.backgroundColor = [UIColor blackColor];
    _maskView.alpha = 0.2;
    [_controlView addSubview:_maskView];
    [self createCurrentTimeLabel];
    [self createTotalTimeLabel];
    [self createCycleButton];
    [self createAboveButton];
    [self createPlayerButton];
    [self createNextButton];
    [self createSlider];
    [self.view addSubview:_controlView];
    [_maskView release];
    [_controlView release];
}

// 显示音乐当前时间
- (void)createCurrentTimeLabel
{
    self.currentTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 30, 20)];
    _currentTimeLabel.backgroundColor = [UIColor clearColor];
    _currentTimeLabel.textColor = [UIColor whiteColor];
    _currentTimeLabel.font = [UIFont systemFontOfSize:15];
    [_controlView addSubview:_currentTimeLabel];
    [_currentTimeLabel release];
}

// 显示音乐总时间
- (void)createTotalTimeLabel
{
    self.totalTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40, 0, 30, 20)];
    _totalTimeLabel.backgroundColor = [UIColor clearColor];
    _totalTimeLabel.textColor = [UIColor whiteColor];
    _totalTimeLabel.font = [UIFont systemFontOfSize:15];
    [_controlView addSubview:_totalTimeLabel];
    [_currentTimeLabel release];
}

// 创建循环按钮
- (void)createCycleButton
{
    self.circleBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, _controlView.frame.size.height / 2 - 12.5, 35, 25)];
    [_circleBtn setImage:[UIImage imageNamed:@"shunxu.png"] forState:UIControlStateNormal];
    [_circleBtn addTarget:self action:@selector(changePlayMode:) forControlEvents:UIControlEventTouchUpInside];
    [_controlView addSubview:_circleBtn];
    [_circleBtn release];
}

// 创建上一首按钮
- (void)createAboveButton
{
    self.aboveBtn = [[UIButton alloc] initWithFrame:CGRectMake(_controlView.frame.size.width / 4 , _controlView.frame.size.height / 2 - 15, 36, 30)];
    [_aboveBtn setImage:[UIImage imageNamed:@"aboveMusic.png"] forState:UIControlStateNormal];
    [_aboveBtn addTarget:self action:@selector(aboveMusic) forControlEvents:UIControlEventTouchUpInside];
    [_controlView addSubview:_aboveBtn];
    [_aboveBtn release];
}

// 创建播放按钮
- (void)createPlayerButton
{
    self.playBtn = [[UIButton alloc] initWithFrame:CGRectMake(_controlView.frame.size.width / 2 - 35, _controlView.frame.size.height / 2 - 26, 70, 52)];
    [_playBtn setImage:[UIImage imageNamed:@"zanting.png"] forState:UIControlStateNormal];
    [_playBtn addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside];
    [_controlView addSubview:_playBtn];
    [_playBtn release];
}

// 创建下一首按钮
- (void)createNextButton
{
    self.nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(_controlView.frame.size.width / 4  * 3 - 36, _controlView.frame.size.height / 2 - 15, 36, 30)];
    [_nextBtn setImage:[UIImage imageNamed:@"nextMusic.png"] forState:UIControlStateNormal];
    [_nextBtn addTarget:self action:@selector(nextMusic) forControlEvents:UIControlEventTouchUpInside];
    [_controlView addSubview:_nextBtn];
    [_nextBtn release];
}

// 创建收藏按钮
- (void)createEnjoyButton
{
    self.enjoyBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 55, self.controlView.frame.size.height / 2 - 12.5, 25, 25)];
    self.isClickEnjoy = NO;
    [_enjoyBtn addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
    [self.controlView addSubview:_enjoyBtn];
    [_enjoyBtn release];
}

// 创建播放进度条
- (void)createSlider
{
    self.progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 120, self.view.frame.size.width, 20)];
    _progressSlider.minimumValue = 0;
    _progressSlider.maximumValue = 1;
    _progressSlider.value = 0;
    [_progressSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    //初始化播放进度条
    [_progressSlider setThumbImage:[UIImage imageNamed:@"dian.png"] forState:UIControlStateNormal];
    [_progressSlider setThumbImage:[UIImage imageNamed:@"dian.png"] forState:UIControlStateHighlighted];
    
    [_progressSlider addTarget:self action:@selector(progressSliderAction:) forControlEvents:UIControlEventValueChanged];
    [_progressSlider addTarget:self action:@selector(progressSliderUp:) forControlEvents:UIControlEventTouchUpInside];
    [_progressSlider addTarget:self action:@selector(progressSliderDown:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:_progressSlider];
    [self.view addSubview:_controlView];
}

// 创建提示播放模式视图
- (void)createPlayModeView
{
    self.playModeView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 80, self.view.frame.size.height / 2 - 30, 160, 60)];
    _playModeView.backgroundColor = [UIColor blackColor];
    _playModeView.layer.cornerRadius = 5;
    _playModeView.alpha = 0.5;
    _playModeView.hidden = YES;
    self.playModeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 80, self.view.frame.size.height / 2 - 10, 160, 20)];
    _playModeLabel.textColor = [UIColor whiteColor];
    _playModeLabel.backgroundColor = [UIColor clearColor];
    _playModeLabel.font = [UIFont boldSystemFontOfSize:15];
    _playModeLabel.textAlignment = UITextAlignmentCenter;
    _playModeLabel.hidden = YES;
    [self.view addSubview:_playModeView];
    [self.view addSubview:_playModeLabel];
    [_playModeLabel release];
    [_playModeView release];
}

#pragma mark -
#pragma mark 事件响应区: 播放 上一首 下一首 循环 进度条

// 创建定时器 监听播放时间
-(void)createTimers:(BOOL)create
{
    if (create) {
        {
            [self createTimers:NO];
            // 每0.1秒触发方法
            self.progressUpdateTimer =
            [NSTimer
             scheduledTimerWithTimeInterval:0.1
             target:self
             selector:@selector(updateProgress:)
             userInfo:nil
             repeats:YES];
        }
    }
    else {
        if (_progressUpdateTimer)
        {
            [_progressUpdateTimer invalidate];
            _progressUpdateTimer = nil;
        }
    }
}

- (void)updateProgress:(NSTimer *)updatedTimer
{
    Singleton * share = [Singleton shareInstance];
    if (share.audioPlayer.bitRate!= 0.0)
    {
        // 当前进度
        double progress = share.audioPlayer.progress;
        // 歌曲总长
        double duration = share.audioPlayer.duration;
        if (duration > 0)
        {
            _currentTimeLabel.text = [NSString stringWithFormat:@"%f", share.audioPlayer.progress];
            _totalTimeLabel.text = [NSString stringWithFormat:@"%f", share.audioPlayer.duration];
            time = share.audioPlayer.progress;
            
            if ((int)share.audioPlayer.progress % 60 < 10) {
                _currentTimeLabel.text = [NSString stringWithFormat:@"%d:0%d",(int)share.audioPlayer.progress / 60, (int)share.audioPlayer.progress % 60];
            } else {
                _currentTimeLabel.text = [NSString stringWithFormat:@"%d:%d",(int)share.audioPlayer.progress / 60, (int)share.audioPlayer.progress % 60];
            }
            if ((int)share.audioPlayer.duration % 60 < 10) {
                _totalTimeLabel.text = [NSString stringWithFormat:@"%d:0%d",(int)share.audioPlayer.duration / 60, (int)share.audioPlayer.duration % 60];
            } else {
                _totalTimeLabel.text = [NSString stringWithFormat:@"%d:%d",(int)share.audioPlayer.duration / 60, (int)share.audioPlayer.duration % 60];
            }

            [_progressSlider setEnabled:YES];
            [_progressSlider setValue:progress / duration];
        }
        else
        {
            [_progressSlider setEnabled:NO];
        }
    }
}

- (void)progressSliderAction:(UISlider *)sender {
    Singleton * share = [Singleton shareInstance];
    double duration = share.audioPlayer.duration;
    double progress = sender.value;
    progress *= duration;
    [share.audioPlayer seekToTime:progress];
}
- (void)progressSliderDown:(UISlider *)sender {
    [_progressUpdateTimer invalidate]; //内存管理
}
- (void)progressSliderUp:(UISlider *)sender {
    Singleton * share = [Singleton shareInstance];
    double duration = share.audioPlayer.duration;
    double progress = sender.value;
    progress *= duration;
    [share.audioPlayer seekToTime:progress];
    [self createTimers:YES];
    [_progressUpdateTimer fire];
}

// 判断播放模式按钮是否被点击
- (void)changePlayMode:(UIButton *)button
{
    if (self.isClickPlayMode == YES) {
        [self.playModeTimer fire];
    }
    if (_playMode != isSingle ) {
        _playMode++;
    }else {
        _playMode = isOrder;
    }
    switch (_playMode) {
        case isOrder:
        {
           [button setImage:[UIImage imageNamed:@"shunxu.png"] forState:UIControlStateNormal];
            [self playModeTimerStartAction:@"播放模式:顺序播放"];
        }
            break;
        case isRandom:
        {
            [button setImage:[UIImage imageNamed:@"suiji.png"] forState:UIControlStateNormal];
            [self playModeTimerStartAction:@"播放模式:随机播放"];
        }
            break;
        case isCycle:
        {
            [button setImage:[UIImage imageNamed:@"xunhuan.png"] forState:UIControlStateNormal];
            [self playModeTimerStartAction:@"播放模式:循环播放"];
        }
            break;
        case isSingle:
        {
            [button setImage:[UIImage imageNamed:@"danqu.png"] forState:UIControlStateNormal];
            [self playModeTimerStartAction:@"播放模式:单曲循环"];
        }
            break;
        default:
            break;
    }
}

// 定时器开启
- (void)playModeTimerStartAction:(NSString *)str
{
    self.isClickPlayMode = YES;
    self.isClickEnjoy = YES;
    self.playModeView.hidden = NO;
    self.playModeLabel.hidden = NO;
    self.playModeLabel.text = str;
    self.playModeTimer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(playModeTimerEndAction) userInfo:nil repeats:YES];
}

// 定时器结束
- (void)playModeTimerEndAction
{
    self.playModeLabel.hidden = YES;
    self.playModeView.hidden = YES;
    [self.playModeTimer invalidate];
}

// 点击向下按钮事件
- (void)downAction:(id)sender
{
    Singleton *share = [Singleton shareInstance];
    share.musicPlayState = MusicPlayStateOtherPlaying;
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 滑动手势
- (void)swipe
{
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDownAction)];
    // 设置清扫的方向, 一个清扫手势只能固定的向一个方向轻扫, 因此, 如果想让一个控件有多个方向的轻扫手势,需要创建多个清扫对象进行添加
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeDown];
    [swipeDown release];
}

- (void)swipeDownAction
{
    Singleton *share = [Singleton shareInstance];
    share.musicPlayState = MusicPlayStateOtherPlaying;
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 点击播放
- (void)playMusic
{
    [self startPlayingMusic];
}

// 上一首
- (void)aboveMusic
{
    _progressSlider.value = 0;
    _currentTimeLabel.text = nil;
    _totalTimeLabel.text = nil;
    _name.text = nil;
    [self abovePlayState];
}

- (void)abovePlayState
{
    Singleton *share = [Singleton shareInstance];
    share.musicPlayState = MusicPlayStateOtherPlaying;
    switch (_playMode) {
        case isOrder:
        {
            if (self.index == 0) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经是第一首了" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView show];
                [alertView release];
            }
            else {
                self.index--;
                [self startPlayingMusic];
            }
        }
            break;
        case isRandom:
        {
            self.index = arc4random() % [_musicArray count] - 1;
            [self startPlayingMusic];
        }
            break;
        case isCycle:
        {
            if (self.index == 0) {
                self.index = [_musicArray count] - 1;
                [self startPlayingMusic];
            }
            else {
                self.index--;
                [self startPlayingMusic];
            }
        }
            break;
        case isSingle:
        {
            self.index = _index;
            [self startPlayingMusic];
        }
            break;
        default:
            break;
    }
}

// 下一首
- (void)nextMusic
{
    _progressSlider.value = 0;
    _currentTimeLabel.text = nil;
    _totalTimeLabel.text = nil;
    _imageUrl = nil;
    [self nextPlayModel];
}

// 判断播放模式
- (void)nextPlayModel
{
    Singleton *share = [Singleton shareInstance];
    share.musicPlayState = MusicPlayStateOtherPlaying;
    switch (_playMode) {
        case isOrder:
        {
            if (self.index == [_musicArray count] - 1) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经没有下一首了" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView show];
                [alertView release];
            }
            else {
                self.index++;
                [self startPlayingMusic];
            }
        }
            break;
        case isRandom:
        {
            self.index = arc4random() % [_musicArray count] - 1;
            [self startPlayingMusic];
        }
            break;
        case isCycle:
        {
            if (self.index == [_musicArray count] - 1) {
                self.index = 0;
                [self startPlayingMusic];
            }
            else {
                self.index++;
                [self startPlayingMusic];
            }
        }
            break;
        case isSingle:
        {
            self.index = _index;
            [self startPlayingMusic];
        }
            break;
        default:
            break;
    }
}

// 判断数据库中此音乐是否存在
- (void)judgeMusicExist
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    Singleton *share = [Singleton shareInstance];
    if ([[user objectForKey:@"state"] isEqualToString:@"sina"]) {
        self.isExist = [MusicDataBase selectForMusicId:share.musicId user:[user objectForKey:@"userSina"]];
    } else if ([[user objectForKey:@"state"] isEqualToString:@"local"]) {
        self.isExist = [MusicDataBase selectForMusicId:share.musicId user:[user objectForKey:@"userName"]];
    }
    
    if (_isExist == NO) {
        [_enjoyBtn setImage:[UIImage imageNamed:@"weishoucang.png"] forState:UIControlStateNormal];
    } else {
        [_enjoyBtn setImage:[UIImage imageNamed:@"yishoucang.png"] forState:UIControlStateNormal];
    }
}

// 改变收藏按钮图片
- (void)changeImage:(UIButton *)button
{
    if (self.isClickEnjoy == YES) {
        [self.playModeTimer fire];
    }
     NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([[user objectForKey:@"login"] isEqualToString:@"yes"]) {
        if ([[user objectForKey:@"state"] isEqualToString:@"sina"]) {
            [self judgeMusicExist];
            if (_isExist == NO) {
                [button setImage:[UIImage imageNamed:@"yishoucang.png"] forState:UIControlStateNormal];
                [self playModeTimerStartAction:@"收藏成功"];
                [MusicDataBase insertIntoMusic:_music user:[user objectForKey:@"userSina"]];
                self.isClickEnjoy = YES;
            } else {
                [button setImage:[UIImage imageNamed:@"weishoucang.png"] forState:UIControlStateNormal];
                self.isClickEnjoy = YES;
                [self playModeTimerStartAction:@"取消收藏"];
                [MusicDataBase deleteFromUserMusic:[_music.trackId stringValue] user:[user objectForKey:@"userSina"]];
            }
        } else if ([[user objectForKey:@"state"] isEqualToString:@"local"])
        {
            [self judgeMusicExist];
            if (_isExist == NO) {
                [button setImage:[UIImage imageNamed:@"yishoucang.png"] forState:UIControlStateNormal];
                [self playModeTimerStartAction:@"收藏成功"];
                [MusicDataBase insertIntoMusic:_music user:[user objectForKey:@"userName"]];
                self.isClickEnjoy = YES;
            } else {
                [button setImage:[UIImage imageNamed:@"weishoucang.png"] forState:UIControlStateNormal];
                self.isClickEnjoy = YES;
                [self playModeTimerStartAction:@"取消收藏"];
                [MusicDataBase deleteFromUserMusic:[_music.trackId stringValue] user:[user objectForKey:@"userName"]];
            }
        }
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)sliderAction:(UISlider *)slider
{
    Singleton * share = [Singleton shareInstance];
    double value = slider.value * share.audioPlayer.duration;
    [share.audioPlayer seekToTime:value];
    if ((int)share.audioPlayer.progress % 60 < 10) {
        _currentTimeLabel.text = [NSString stringWithFormat:@"%d:0%d",(int)share.audioPlayer.progress / 60, (int)share.audioPlayer.progress % 60];
    } else {
        _currentTimeLabel.text = [NSString stringWithFormat:@"%d:%d",(int)share.audioPlayer.progress / 60, (int)share.audioPlayer.progress % 60];
    }
}

- (void)showTime {
    Singleton * share = [Singleton shareInstance];
    
    _currentTimeLabel.text = [NSString stringWithFormat:@"%f", share.audioPlayer.progress];
    _totalTimeLabel.text = [NSString stringWithFormat:@"%f", share.audioPlayer.duration];
    time = share.audioPlayer.progress;
    //动态更新进度条时间
    if ((int)share.audioPlayer.progress % 60 < 10) {
        _currentTimeLabel.text = [NSString stringWithFormat:@"%d:0%d",(int)share.audioPlayer.progress / 60, (int)share.audioPlayer.progress % 60];
    } else {
        _currentTimeLabel.text = [NSString stringWithFormat:@"%d:%d",(int)share.audioPlayer.progress / 60, (int)share.audioPlayer.progress % 60];
    }
    if ((int)share.audioPlayer.duration % 60 < 10) {
        _totalTimeLabel.text = [NSString stringWithFormat:@"%d:0%d",(int)share.audioPlayer.duration / 60, (int)share.audioPlayer.duration % 60];
    } else {
        _totalTimeLabel.text = [NSString stringWithFormat:@"%d:%d",(int)share.audioPlayer.duration / 60, (int)share.audioPlayer.duration % 60];
    }
    _progressSlider.value = share.audioPlayer.progress / share.audioPlayer.duration;
}

#pragma mark -
#pragma mark 监听音乐状态
- (void)startMonitor
{
    // 监听歌曲是播放完成
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(musicState:) name:@"ASStatusChangedNotification" object:nil];
}

//  改变状态
- (void)musicState:(NSNotification *)nc
{
    if ([[nc.userInfo objectForKey:@"state"] isEqualToString:@"8"]) {
        [self nextPlayModel];
    }
    if ([[nc.userInfo objectForKey:@"state"] isEqualToString:@"2"] || [[nc.userInfo objectForKey:@"state"] isEqualToString:@"4"] || _currentTimeLabel.text == nil) {
        _aboveBtn.enabled = NO;
        _nextBtn.enabled = NO;
    }else {
        _nextBtn.enabled = YES;
        _aboveBtn.enabled = YES;
    }
    
    Singleton *share = [Singleton shareInstance];
    if (share.audioPlayer.errorCode != 0) {
        _nextBtn.enabled = YES;
        _aboveBtn.enabled = YES;
    }
}

#pragma mark -
#pragma mark 音乐播放
- (void)startPlayingMusic
{
    Singleton *share = [Singleton shareInstance];
    switch (share.musicPlayState) {
        case MusicPlayStateStart:
        {
            [share.audioPlayer pause];
            [_playBtn setImage:[UIImage imageNamed:@"play-1.png"] forState:UIControlStateNormal];
            share.musicPlayState = MusicPlayStatePause;
        }
            break;
        case MusicPlayStatePause:
        {
            [share.audioPlayer start];
            [_playBtn setImage:[UIImage imageNamed:@"zanting.png"] forState:UIControlStateNormal];
            share.musicPlayState = MusicPlayStateStart;
        }
            break;
        case MusicPlayStateNone:
        {
            share.audioPlayer = [[AudioStreamer alloc] initWithURL:[NSURL URLWithString:_music.playUrl64]];
            [share.audioPlayer start];
            [_playBtn setImage:[UIImage imageNamed:@"zanting.png"] forState:UIControlStateNormal];
            share.musicPlayState = MusicPlayStateStart;
            [self createTimers:YES];
        }
            break;
        case MusicPlayStateOtherPlaying:
        {
            [share.audioPlayer stop];
            [share.audioPlayer release];
            
            switch (_buttonState) {
                case AboveButton:
                {
                    _music = [_musicArray objectAtIndex:self.index];
                    _buttonState = PlayButton;
                }
                    break;
                case NextButton:
                {
                    _music = [_musicArray objectAtIndex:self.index];
                    _buttonState = PlayButton;
                }
                    break;
                case PlayButton:
                {
                    _music = [_musicArray objectAtIndex:self.index];
                }
                    break;
                default:
                    break;
            }
            share.audioPlayer = [[AudioStreamer alloc] initWithURL:[NSURL URLWithString:_music.playUrl64]];
            [share.audioPlayer start];
            [_playBtn setImage:[UIImage imageNamed:@"zanting.png"] forState:UIControlStateNormal];
            share.musicPlayState = MusicPlayStateStart;
            [self createTimers:YES];
        }
            break;
        default:
            break;
    }
    share.musicId = [_music.trackId stringValue];
    _name.text = _music.title;
    [self judgeMusicExist];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    LoginViewCnntroller *login = [[LoginViewCnntroller alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
    [self presentViewController:nav animated:YES completion:nil];
    [login release];
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
